#!/bin/bash

set -e
pushd $(cd $(dirname ${0})/..; pwd) > /dev/null

src_dir="./third_party/source/llama.cpp"

clean() {
    echo "ggml clean"
    rm -rf "${src_dir}"
}

sync() {
    echo "ggml sync"
    if [ ! -d "${src_dir}" ]; then
        git clone --quiet --depth=1 --branch master --single-branch https://github.com/ggerganov/llama.cpp.git "${src_dir}"
    else
        git -C "${src_dir}" fetch --quiet --depth=1 origin master
        git -C "${src_dir}" reset --quiet --hard FETCH_HEAD
        git -C "${src_dir}" gc --quiet --prune=all
    fi
}

while [ "$#" != 0 ]; do
    command="$1" && shift
    case "${command}" in
        "clean") clean;;
        "sync") sync;;
    esac
done
